package com.Action;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.Entity.DataInfo;
import com.Entity.InterestInfo;
import com.Entity.UsersInfo;
import com.Entity.Page.Json;
import com.Service.InterestInfoService;
import com.Service.UserService;

public class UserAction extends BaseAction {

	private int flag = 0;
	private String msg;

	@Autowired
	private UserService userService;
	@Autowired
	private InterestInfoService interestService;

	private List<UsersInfo> userlist = new ArrayList<UsersInfo>();
	private List<DataInfo> datalist = new ArrayList<DataInfo>();
	DataInfo datainfo = new DataInfo();
	UsersInfo userinfo = new UsersInfo();
	InterestInfo interest;

	public UsersInfo getUsersinfo() {
		return userinfo;
	}

	public void setUsersinfo(UsersInfo userinfo) {
		this.userinfo = userinfo;
	}

	public DataInfo getDatainfo() {
		return datainfo;
	}

	public void setDatainfo(DataInfo datainfo) {
		this.datainfo = datainfo;
	}

	public List<UsersInfo> getUserlist() {
		return userlist;
	}

	public void setUserlist(List<UsersInfo> userlist) {
		this.userlist = userlist;
	}

	public List<DataInfo> getDatalist() {
		return datalist;
	}

	public void setDatalist(List<DataInfo> datalist) {
		this.datalist = datalist;
	}

	// 登录
	public void login() throws UnsupportedEncodingException {

		String login = ServletActionContext.getRequest().getParameter("login");
		String password = ServletActionContext.getRequest().getParameter(
				"password");

		login = new String(login.getBytes("ISO-8859-1"), "UTF-8");

		login = java.net.URLDecoder.decode(login, "UTF-8");
		password = java.net.URLDecoder.decode(password, "UTF-8");

		this.userlist = userService.checkUser(login, password);
		Json json = new Json();
		if (userlist.isEmpty() || userlist.size() == 0) {
			json.setSuccess(false);
			json.setMsg("用户不存在或密码错误！");
		} else {
			this.userinfo = userlist.get(0);
			json.setObj(userinfo);
			json.setSuccess(true);
			json.setMsg("登录成功！");
		}
		writeJson(json);
	}

	// 注册
	public void register() throws UnsupportedEncodingException, ParseException {

		String username = ServletActionContext.getRequest().getParameter(
				"username");
		String phonenum = ServletActionContext.getRequest().getParameter(
				"phonenum");
		String password = ServletActionContext.getRequest().getParameter(
				"password");
		String deviceId = ServletActionContext.getRequest().getParameter(
				"deviceId");
		String gender = ServletActionContext.getRequest()
				.getParameter("gender");
		String weight = ServletActionContext.getRequest()
				.getParameter("weight");
		String height = ServletActionContext.getRequest()
				.getParameter("height");
		String interestTag = ServletActionContext.getRequest().getParameter(
				"interestTag");

		username = new String(username.getBytes("ISO-8859-1"), "UTF-8");
		username = java.net.URLDecoder.decode(username, "UTF-8");
		interestTag = new String(interestTag.getBytes("ISO-8859-1"), "UTF-8");
		interestTag = java.net.URLDecoder.decode(interestTag, "UTF-8");

		flag = userService.findByUserName(username);
		userlist = userService.checkLogin(phonenum, password);

		Json json = new Json();
		if (flag != 0) {
			json.setSuccess(false);
			json.setMsg("用户名已存在！");
		} else if (userlist != null && userlist.size() != 0) {
			json.setSuccess(false);
			json.setMsg("手机号和密码重复，请改掉其中之一！");
		} else {
			UsersInfo userinfo = new UsersInfo();

			userinfo.setUserName(username);
			userinfo.setCellphone(phonenum);
			userinfo.setPassword(password);
			userinfo.setDeviceId(Integer.valueOf(deviceId));
			userinfo.setSex(Short.valueOf(gender));
			userinfo.setWeight(Float.valueOf(weight));
			userinfo.setHeight(Float.valueOf(height));
			userinfo.setInterestTag(interestTag);
			float bmi = (float) (userinfo.getWeight() / Math.pow(
					(userinfo.getHeight() / 100), 2));
			userinfo.setBmi(bmi);
			this.userService.save(userinfo);

			// 保存用户兴趣模型
			String interest_arr[] = interestTag.split("\\#");
			List<UsersInfo> userlist = userService.findByPhoneNum(userinfo
					.getCellphone());
			int userid = userlist.get(0).getUserId();
			for (String str : interest_arr) {
				interest = new InterestInfo();
				interest.setInterestName(str);
				interest.setUserId(userid);
				interest.setInterestWeight(1);
				interest.setIsChannel((short) 1);
				interest.setCreateTime(new java.sql.Timestamp(System
						.currentTimeMillis()));
				interest.setModifyTime(new java.sql.Timestamp(System
						.currentTimeMillis()));
				interestService.save(interest);
			}

			InterestInfo pre_interest = new InterestInfo();
			// 根据BMI数值，推测用户兴趣特征项
			if (bmi >= 28) {
				pre_interest.setInterestName("胖");
				pre_interest.setInterestWeight(0.8f);
			} else if (bmi < 28 && bmi > 24) {
				pre_interest.setInterestName("胖");
				pre_interest.setInterestWeight(0.6f);
			} else if (bmi <= 22 && bmi >= 18.5) {
				pre_interest.setInterestName("瘦");
				pre_interest.setInterestWeight(0.6f);
			} else if (bmi < 18.5) {
				pre_interest.setInterestName("瘦");
				pre_interest.setInterestWeight(0.8f);
			}
			pre_interest.setIsChannel((short) 0);
			pre_interest.setUserId(userid);
			pre_interest.setCreateTime(new java.sql.Timestamp(System
					.currentTimeMillis()));
			pre_interest.setModifyTime(new java.sql.Timestamp(System
					.currentTimeMillis()));
			interestService.save(pre_interest);

			json.setSuccess(true);
			json.setMsg("注册成功！");
		}
		writeJson(json);
	}

	// 显示个人资料
	public void displayPersonal() throws UnsupportedEncodingException {
		String userid = ServletActionContext.getRequest()
				.getParameter("userid");

		int id = Integer.parseInt(userid);

		Json json = new Json();
		userinfo = this.userService.getById(id);
		if (userinfo != null) {
			json.setObj(userinfo);
			json.setSuccess(true);
		} else {
			json.setSuccess(false);
			json.setMsg("获取资料失败！");
		}
		writeJson(json);
	}

	// 修改个人资料
	public void updatePersonal() throws UnsupportedEncodingException,
			ParseException {
		String userid = ServletActionContext.getRequest()
				.getParameter("userid");
		String username = ServletActionContext.getRequest().getParameter(
				"username");
		String phonenum = ServletActionContext.getRequest().getParameter(
				"phonenum");
		String deviceid = ServletActionContext.getRequest().getParameter(
				"deviceid");
		String gender = ServletActionContext.getRequest()
				.getParameter("gender");
		String weight = ServletActionContext.getRequest()
				.getParameter("weight");
		String birthday = ServletActionContext.getRequest().getParameter(
				"birthday");
		String height = ServletActionContext.getRequest()
				.getParameter("height");
		String interestTag = ServletActionContext.getRequest().getParameter(
				"interestTag");

		username = new String(username.getBytes("ISO-8859-1"), "UTF-8");

		username = java.net.URLDecoder.decode(username, "UTF-8");

		Json json = new Json();
		userinfo = this.userService.getById(Integer.parseInt(userid));
		if (userinfo != null) {
			if (null != username && !username.isEmpty()) {
				userinfo.setUserName(username);
			}

			if (null != phonenum && !phonenum.isEmpty()) {
				userinfo.setCellphone(phonenum);
			}

			if (null != deviceid && !deviceid.isEmpty()) {
				userinfo.setDeviceId(Integer.valueOf(deviceid));
			}

			if (null != gender && !gender.isEmpty()) {
				userinfo.setSex(Short.valueOf(gender));
			}

			if (null != weight && !weight.isEmpty()) {
				userinfo.setWeight(Float.valueOf(weight));
			}
			if (null != height && !height.isEmpty()) {
				userinfo.setHeight(Float.valueOf(height));
			}
			if (userinfo.getWeight() > 0 && userinfo.getHeight() > 0) {
				float bmi = (float) (userinfo.getWeight() / Math.pow(
						(userinfo.getHeight() / 100), 2));
				userinfo.setBmi(bmi);
			}
			if (null != interestTag && !interestTag.isEmpty()) {
				userinfo.setInterestTag(interestTag);
			}
			System.out.println("birthday:" + birthday);

			Date birthday1 = new Date();
			if (null != birthday && !birthday.equals("")) {
				DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
				birthday1 = (Date) fmt.parse(birthday);
				userinfo.setBirthday(birthday1);
			}
			this.userService.save(userinfo);
			json.setSuccess(true);
			json.setMsg("修改成功!");
		} else {
			json.setSuccess(false);
			json.setMsg("修改失败!");
		}
		writeJson(json);
	}

	// 修改密码
	public void updatePassword() throws UnsupportedEncodingException {
		String userid = ServletActionContext.getRequest()
				.getParameter("userid");
		String oldpassword = ServletActionContext.getRequest().getParameter(
				"oldpassword");
		String newpassword = ServletActionContext.getRequest().getParameter(
				"newpassword");

		int id = Integer.parseInt(userid);
		Json json = new Json();
		userinfo = this.userService.getById(id);
		if (userinfo != null) {
			String password = userinfo.getPassword();
			if (!oldpassword.equals(password)) {
				json.setSuccess(false);
				json.setMsg("密码不正确！");
			} else {
				userinfo.setPassword(newpassword);
				this.userService.save(userinfo);
				json.setSuccess(true);
				json.setMsg("修改成功！");
			}
		} else {
			json.setSuccess(false);
			json.setMsg("修改失败！");
		}
		writeJson(json);
	}

}
